package cn.com.lsy.product.lepao.util.payUtil.weChatPayUtil;


import java.io.IOException;
import java.io.StringReader;
import java.util.List;


import cn.com.lsy.product.lepao.util.payUtil.weChatPayUtil.weChatPayEntity.UnifiedorderResult;
import cn.com.lsy.product.lepao.util.payUtil.weChatPayUtil.weChatPayEntity.WXPayResult;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;


/**
 * ΢�Ž���xml������CDATA��ʽ��
 *
 * @author iYjrg_xiebin
 * @date 2015��11��26������10:18:07
 */
public class JdomParseXmlUtils {
	
	/*//**
            *1��ͳһ�µ���ȡ΢�ŷ���
            * ������ʱ���Զ�ȥ��CDMA
	 *
    @param
    xml
	 *//*/

    @SuppressWarnings("unchecked")
    public static UnifiedorderResult getUnifiedorderResult(String xml) {
        UnifiedorderResult unifieorderResult = new UnifiedorderResult();
        try {
            StringReader read = new StringReader(xml);
            // �����µ�����ԴSAX ��������ʹ�� InputSource ������ȷ����ζ�ȡ XML ����
            InputSource source = new InputSource(read);
            // ����һ���µ�SAXBuilder
            SAXBuilder sb = new SAXBuilder();
            // ͨ������Դ����һ��Document
            Document doc;
            doc = (Document) sb.build(source);

            Element root = doc.getRootElement();// ָ����ڵ�
            List<Element> list = root.getChildren();

            if (list != null && list.size() > 0) {
                for (Element element : list) {

                    System.out.println("key�ǣ�" + element.getName() + "��ֵ�ǣ�" + element.getText());

                    if ("return_code".equals(element.getName())) {
                        unifieorderResult.setReturn_code(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        unifieorderResult.setReturn_msg(element.getText());
                    }

                    if ("appid".equals(element.getName())) {
                        unifieorderResult.setAppid(element.getText());
                    }


                    if ("mch_id".equals(element.getName())) {
                        unifieorderResult.setMch_id(element.getText());
                    }

                    if ("nonce_str".equals(element.getName())) {
                        unifieorderResult.setNonce_str(element.getText());
                    }

                    if ("sign".equals(element.getName())) {
                        unifieorderResult.setSign(element.getText());
                    }

                    if ("result_code".equals(element.getName())) {
                        unifieorderResult.setResult_code(element.getText());
                    }

                    if ("prepay_id".equals(element.getName())) {
                        unifieorderResult.setPrepay_id(element.getText());
                    }

                    if ("trade_type".equals(element.getName())) {
                        unifieorderResult.setTrade_type(element.getText());
                    }
                }
            }

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return unifieorderResult;
    }
	
	
	/*/

    /**
     * 2��΢�Żص����������
     * ������ʱ���Զ�ȥ��CDMA
     *
     * @param xml //
     **/

    @SuppressWarnings("unchecked")
    public static WXPayResult getWXPayResult(String xml) {
        WXPayResult wXPayResult = new WXPayResult();
        try {
            StringReader read = new StringReader(xml);
            // �����µ�����ԴSAX ��������ʹ�� InputSource ������ȷ����ζ�ȡ XML ����
            InputSource source = new InputSource(read);
            // ����һ���µ�SAXBuilder
            SAXBuilder sb = new SAXBuilder();
            // ͨ������Դ����һ��Document
            Document doc;
            doc = (Document) sb.build(source);

            Element root = doc.getRootElement();// ָ����ڵ�
            List<Element> list = root.getChildren();

            if (list != null && list.size() > 0) {
                for (Element element : list) {
                    System.out.println("key�ǣ�" + element.getName() + "��ֵ�ǣ�" + element.getText());

                    if ("return_code".equals(element.getName())) {
                        wXPayResult.setReturn_code(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }


                    if ("appid".equals(element.getName())) {
                        wXPayResult.setAppid(element.getText());
                    }

                    if ("mch_id".equals(element.getName())) {
                        wXPayResult.setMch_id(element.getText());
                    }

                    if ("nonce_str".equals(element.getName())) {
                        wXPayResult.setNonce_str(element.getText());
                    }

                    if ("sign".equals(element.getName())) {
                        wXPayResult.setSign(element.getText());
                    }

                    if ("result_code".equals(element.getName())) {
                        wXPayResult.setResult_code(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        wXPayResult.setReturn_msg(element.getText());
                    }

                }
            }

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return wXPayResult;
    }

}
